<?php
function theme_extra_param($element) {
  /*
	 * data will store like : array(
	 *                          'type' => 'block' (node, view),
	 *                          'block' => array(block id => '', 'hidden title => ')
	 *                          'node' => node_id
	 *                          'view' => array(id => 'view id', 'agruments' => 'agruments') 
	 *                        )
	 */
  $default_value = isset ( $element ['#default_value'] ) ? $element ['#default_value'] : array ();
  
  $type = isset ( $default_value ['type'] ) ? $default_value ['type'] : NULL;
  $output = '';
  $output .= '<div class="form-item">';
  $output .= '<label>' . t ( 'Select content type:' ) . '</label>';
  
  $output .= '<select name="' . $element ['#name'] . '[type]" id="' . $element ['#id'] . '-type">';
  
  $output .= '<option value="none">' . t ( 'None' ) . '</option>';
  $has = array ();
  if (empty ( $element ['#block'] ) || $element ['#block'] != 0) {
    $has_block = 1;
    $selected = ($type == 'block') ? 'selected' : '';
    $output .= '<option ' . $selected . ' value="block">' . t ( 'Block' ) . '</option>';
  }
  
  if (empty ( $element ['#node'] ) || $element ['#node'] != 0) {
    $has_node = 1;
    $selected = ($type == 'node') ? 'selected' : '';
    $output .= '<option ' . $selected . ' value="node">' . t ( 'Node' ) . '</option>';
  }
  
  if (empty ( $element ['#view'] ) || $element ['#view'] != 0) {
    if (module_exists ( 'view' )) {
      $has_view = 1;
      $selected = ($type == 'view') ? 'selected' : '';
      $output .= '<option ' . $selected . ' value="view">' . t ( 'view' ) . '</option>';
    }
  }
  
  $output .= '</select>';
  $output .= '<div class="description">' . $element ['#description'] . '</description>';
  $output .= '</div>';
  if ($has_block) {
    $bid = isset ( $default_value ['block'] ['id'] ) ? $default_value ['block'] ['id'] : NULL;
    $output .= '<div class="block">';
    $output .= '<label>' . t ( 'Choose Block' ) . '</label>';
    $output .= '<select name="' . $element ['#name'] . '[block][id]" id="' . $element ['#id'] . '-block-id">';
    $blocks = _flexmenu_get_blocks ();
    foreach ( $blocks as $value => $text ) {
      $selected = '';
      if ($value == $bid) {
        $selected = 'selected';
      }
      $output .= '<option ' . $selected . ' value="' . $value . '">' . $text . '</option>';
    }
    $output .= '</select>';
    
    $checked = '';
    if (isset ( $default_value ['block'] ['title'] )) {
      $checked = 'checked="checked"';
    }
    $output .= '<div><input ' . $checked . ' type="checkbox" name="' . $element ['#name'] . '[block][title]" value="1" />';
    $output .= '<span>' . t ( 'Hidden title' ) . '</span></div>';
    $output .= '</div>';
  }
  
  if ($has_node) {
    $output .= '<div class="node">';
    $output .= '<label>' . t ( 'Enter node id' ) . '</label>';
    $output .= '<input name="' . $element ['#name'] . '[node][id]" id="' . $element ['#id'] . '-node-id" />';
    
    $output .= '<div><input type="checkbox" name="' . $element ['#name'] . '[node][teaser]" value="1" />';
    $output .= '<span>' . t ( 'Only load teaser' ) . '</span></div>';
    $output .= '</div>';
  }
  
  $js = "$(document).ready(function(){
      f_menu_additional_type('{$element ['#id']}-type');
      $('#{$element ['#id']}-type').change(function(){
        f_menu_additional_type('{$element ['#id']}-type');
      });
  });";
  drupal_add_js ( $js, 'inline' );
  return $output;
}

function _flexmenu_get_blocks() {
  static $blocksarray;
  if (empty ( $blocksarray )) {
    global $theme_key;
    init_theme ();
    $result = db_query ( "SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key );
    $old_blocks = array ();
    while ( $old_block = db_fetch_array ( $result ) ) {
      $old_blocks [$old_block ['module']] [$old_block ['delta']] = $old_block;
    }
    $blocks = array ();
    foreach ( module_list () as $module ) {
      $module_blocks = module_invoke ( $module, 'block', 'list' );
      if ($module_blocks) {
        foreach ( $module_blocks as $delta => $block ) {
          if (empty ( $old_blocks [$module] [$delta] )) {
            // If it's a new block, add identifiers.
            $block ['module'] = $module;
            $block ['delta'] = $delta;
            $block ['theme'] = $theme_key;
            $blocks [] = $block;
          } else {
            $old_blocks [$module] [$delta] ['info'] = $block ['info'];
            // Add this block to the list of blocks we return.
            $blocks [] = $old_blocks [$module] [$delta];
          }
        }
      }
    }
    $blocksarray = array ();
    foreach ( $blocks as $i => $block ) {
      if ($block ['module'] != 'quicktabs') {
        $key = $block ['module'] . '_delta_' . $block ['delta'];
        $blocksarray [$key] = $block ['info'];
      }
    }
  }
  return $blocksarray;
}

function theme_iconfield($element) {
  $base_path = $element ['#base_path'];
  if (! file_exists ( $base_path ))
    return;
  
  $files = file_scan_directory ( $base_path, '\.icon$|\.png$|\.gif$' );
  
  $options = array ();
  
  $output = '<div class="form-item">';
  $output .= '<label>' . t ( $element ['#title'] ) . ':</label>';
  $output .= '<div class="clear-block">';
  $output .= '<select name="' . $element ['#name'] . '" id="' . $element ['#id'] . '">';
  
  $output .= '<option value="0">' . t ( '-- Select Image --' ) . '</option>';
  foreach ( $files as $path => $info ) {
    $selected = '';
    if ($element ['#default_value'] == $path) {
      $selected = 'selected';
    }
    $output .= '<option ' . $selected . ' value="' . $path . '">' . $info->basename . '</option>';
  }
  
  $output .= '</select>';
  $output .= '</div>';
  $output .= '<div class="description">' . $element ['#description'] . '</div>';
  
  $output .= '</div>';
  
  return $output;
}

function template_preprocess_flexmenu_tree(&$vars) {
  $link = flexmenu::$connection ['link'];
  $menu_name = $link ['menu_name'];
  
  $type = flexmenu::$types [$menu_name];
  $m_settings = flexmenu::$m_settings [$menu_name];
  $direction = $m_settings['direction'];
  $duration = $m_settings['duration'];
  
  $vars ['level'] = $link ['depth'] - 1;
  $vars ['type'] = $type;
  $vars ['menu_name'] = $menu_name;
  $vars ['direction'] = $direction;
  
  $plid = $link ['plid'];
  $has_cols = false;
  if (isset ( flexmenu::$connection ['cols'] [$plid] )) {
    $has_cols = true;
  }
  $vars ['has_cols'] = $has_cols;
  
  //try to find block, node or view add to this
  $p_settings = isset ( flexmenu::$params [$menu_name] [$plid] ) ? unserialize ( flexmenu::$params [$menu_name] [$plid]->settings ) : array ();
  if (isset ( $p_settings ['extra'] ['type'] )) {
    switch ($p_settings ['extra'] ['type']) {
      case 'block' :
        $bid = $p_settings ['extra'] ['block'] ['id'];
        $pos = strpos ( $bid, '_delta_' );
        $blockmodule = substr ( $bid, 0, $pos );
        $blockdelta = substr ( $bid, $pos + 7 );
        $block = ( object ) module_invoke ( $blockmodule, 'block', 'view', $blockdelta );
        if (isset ( $block->content )) {
          $block->module = $blockmodule;
          $block->delta = $blockdelta;
          $block->region = 'quicktabs_tabpage';
          if ($p_settings ['extra'] ['block'] || ! isset ( $block->subject )) {
            $block->subject = FALSE;
          }
          
          $output = theme ( 'block', $block );
        }
        
        $vars ['extra_content'] = $output;
        break;
      default :
        break;
    }
  }
  
  //try to add dynamic js and css
  if (! defined ( 'MENU_' . $type )) {
    define ( 'MENU_' . $type, '1' );
    $module_path = drupal_get_path ( 'module', 'flexmenu' );
  
  }
  
  
  if (! defined ( 'MENU_' . $type . '_' . $menu_name )) {
    define ( 'MENU_' . $type . '_' . $menu_name, 1 );
    $js = '$(document).ready(function(){
         $("#megamenu-' . $menu_name . '").megamenu({
           direction: "' . $direction . '",
           duration : "'.$duration.'",
           index: "'.$m_settings['index'].'",
           animateOver: "'.$m_settings['animate_over'].'",
           animateOut : "'.$m_settings['animate_out'].'"
         });
      });';
    
    
    drupal_add_js ( $js, 'inline' );
  }
  
  if ($type)
    $vars ['template_files'] [] = 'flexmenu-tree-' . $type;
}

function flexmenu_menu_tree_render($tree) {
  global $theme;
  $link = flexmenu::$connection ['link'];
  $menu_name = $link ['menu_name'];
  
  $registry = variable_get ( 'flexmenu_theme', array () );
  $function = isset ( $registry [$theme] ) ? $registry [$theme] ['menu_tree'] : 'theme_menu_tree';
  
  if (! flexmenu::$types [$menu_name]) {
    return $function ( $tree );
  }
  
  return theme ( array ('flexmenu_tree__' . flexmenu::$types [$menu_name], 'flexmenu_tree' ), $link, $tree );
}

function template_preprocess_flexmenu(&$vars) {
  $link = $vars ['org_link'];
  $menu_name = $link ['menu_name'];
  
  $mlid = $link ['mlid'];
  $plid = $link ['plid'];
  //get colums settings and give it for template file
  if (isset ( flexmenu::$connection ['cols'] [$plid] )) {
    $cols = flexmenu::$connection ['cols'] [$plid];
    $vars ['cols'] = $cols;
  }
  
  //get extra setting
  $extra_settings = flexmenu::$params [$menu_name] [$mlid];
  
  $settings = unserialize ( $extra_settings->settings );
  $vars ['settings'] = $settings;
  
  if (isset ( $settings ['extra'] ['type'] )) {
    switch ($settings ['extra'] ['type']) {
      case 'block' :
        $bid = $settings ['extra'] ['block'] ['id'];
        $pos = strpos ( $bid, '_delta_' );
        $blockmodule = substr ( $bid, 0, $pos );
        $blockdelta = substr ( $bid, $pos + 7 );
        $block = ( object ) module_invoke ( $blockmodule, 'block', 'view', $blockdelta );
        if (isset ( $block->content )) {
          $block->module = $blockmodule;
          $block->delta = $blockdelta;
          $block->region = 'quicktabs_tabpage';
          if ($settings ['extra'] ['block'] || ! isset ( $block->subject )) {
            $block->subject = FALSE;
          }
          
          $output = theme ( 'block', $block );
        }
        
        $vars ['extra_content'] = $output;
        break;
      default :
        break;
    }
  }
  $vars ['level'] = $link ['depth'] - 1;
  $vars ['in_active_trail'] = $vars ['in_active_trail'];
  $vars ['extra_class'] = $vars ['extra_class'];
  
  $type = flexmenu::$types [$menu_name];
  
  if ($type)
    $vars ['template_files'] [] = 'flexmenu-' . $type;
}

function flexmenu_menu_item_render($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  global $theme;
  
  //$org_link = _flexmenu_link_stack();
  $org_link = flexmenu::stack ();
  
  $menu_name = $org_link ['menu_name'];
  
  $registry = variable_get ( 'flexmenu_theme', array () );
  $function = isset ( $registry [$theme] ) ? $registry [$theme] ['menu_item'] : 'theme_menu_item';
  if (! flexmenu::$types [$menu_name]) {
    return $function ( $link, $has_children, $menu, $in_active_trail, $extra_class );
  }
  
  return theme ( array ('flexmenu__' . flexmenu::$types [$menu_name], 'flexmenu' ), $org_link, $link, $has_children, $menu, $in_active_trail, $extra_class );
}

function template_preprocess_flexmenu_link(&$vars) {
  global $base_path;
  
  $link = & $vars ['link'];
  $mlid = $link ['mlid'];
  
  $menu_name = $link ['menu_name'];
  
  $type = flexmenu::$types [$menu_name];
  $param = flexmenu::$params [$menu_name] [$mlid];
  
  $settings = isset ( $param->settings ) ? unserialize ( $param->settings ) : array ();
  if ($settings ['icon']) {
    $settings ['icon'] = $base_path . $settings ['icon'];
  }
  
  $vars ['settings'] = $settings;
  
  if (empty ( $link ['localized_options'] )) {
    $link ['localized_options'] = array ();
  }
  $link ['localized_options'] += array ('html' => TRUE );
  
  if ($type)
    $vars ['template_files'] [] = 'flexmenu-link-' . $type;
}

function flexmenu_menu_item_link_render($link) {
  global $theme;
  //_flexmenu_link_stack($link);
  flexmenu::stack ( $link );
  
  $menu_name = $link ['menu_name'];
  $registry = variable_get ( 'flexmenu_theme', array () );
  $function = isset ( $registry [$theme] ) ? $registry [$theme] ['menu_item_link'] : 'theme_menu_item_link';
  if (! flexmenu::$types [$menu_name]) {
    return $function ( $link );
  }
  
  $type = flexmenu::$types [$menu_name];
  
  return theme ( array ('flexmenu_link__' . $type, 'flexmenu_link' ), $link );
}